package wms

import (
	"context"
	"encoding/json"
	"ez/apps/wms/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type ItemAttribute struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64         `json:"id" bson:"id,omitempty"`
	ItemBase    *ItemBase     `json:"itemBase" bson:"itemBase"`       //物品
	ItemBaseId  int64         `json:"itemBaseId" bson:"itemBaseId"`   //物品
	Attribute   *AttributeKey `json:"attribute" bson:"attribute"`     //属性
	AttributeId int64         `json:"attributeId" bson:"attributeId"` //属性
	Name        string        `json:"name" bson:"name"`               //属性名称
	Values      string        `json:"values" bson:"values"`           //属性值
	Code        string        `json:"code" bson:"code"`               //编码
	Sort        int64         `json:"sort" bson:"sort"`               //排序
	CreateAt    time.Time     `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time     `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time    `json:"deleteAt" bson:"deleteAt"`
}

func (this *ItemAttribute) DocName() string { return "ItemAttribute" }
func (this *ItemAttribute) GetId() int64    { return this.Id }
func (this *ItemAttribute) SetId(id int64)  { this.Id = id }
func (this *ItemAttribute) Create() error {
	return this.GetFactory().Create(this)
}
func (this *ItemAttribute) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *ItemAttribute) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *ItemAttribute) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *ItemAttribute) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *ItemAttribute) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *ItemAttribute) ToString() string {
	return string(this.ToBytes())
}
func (this *ItemAttribute) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ItemAttribute) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ItemAttribute) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *ItemAttribute) LoadItemBase() {
	if this.ItemBaseId == 0 {
		return
	}
	this.ItemBase, _ = NewItemBaseCrud().FindId(this.ItemBaseId)
}
func (this *ItemAttribute) LoadAttribute() {
	if this.AttributeId == 0 {
		return
	}
	this.Attribute, _ = NewAttributeKeyCrud().FindId(this.AttributeId)
}
func (this *ItemAttribute) ClearRelationsBeforeSave() mgo.Doc {
	this.ItemBase = nil
	this.Attribute = nil
	return this
}
func neverUsed_ItemAttribute() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type ItemAttributeAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *ItemAttribute
	Session *ez.Session
}

func NewItemAttributeAccessControl(model *ItemAttribute, action string, session *ez.Session) *ItemAttributeAccessControl {
	ctrl := &ItemAttributeAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.ItemAttributeAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
